//
//  RBGoodsOrderListVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/12.
//

#import "RBGoodsOrderListVC.h"
#import "RBGoodsOrderResponse.h" //model
#import "RBGoodsOrderListCell.h" //cell
#import "RBGoodsOrderDetailVC.h" //订单详情
#import "RBCashierVC.h" //收银台
#import "RBPublishEvaluateVC.h" //评价
#import "RBAfterSalesAlertView.h"
#import "RBLogisticsVC.h"

@interface RBGoodsOrderListVC ()
<UICollectionViewDataSource, UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout,
RBGoodsOrderDetailDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, strong) RBGoodsOrderResponse *response;

@end

@implementation RBGoodsOrderListVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(10), 0, Zoom(10), 0);
        group.minimumLineSpacing = Zoom(10);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
}

- (void)appendData {
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.lastObject;
        (NSMutableArray *)group.cellModels;
    });
    LCWeakSelf;
    for (RBOrderListModel *model in self.response.data) {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBGoodsOrderListCell.class);
            cellModel.cellData = model;
            cellModel.eventBlock1 = ^(NSInteger type, NSInteger index, RBOrderListModel *data) {
                switch (type) {
                    case 1 : { //去支付
                        [weakSelf payOrderWith:data];
                    }   break;
                    case 2 : { //查看订单
                        [weakSelf checkOrderWith:data];
                    }   break;
                    case 3 : { //取消订单
                        [weakSelf cancelOrderWith:data];
                    }   break;
                    case 4 : { //查看物流
                        [weakSelf checkLogisticsWith:data];
                    }   break;
                    case 5 : { //去评价
                        [weakSelf appraiseWith:data];
                    }   break;
                    case 6 : { //确认收货
                        [weakSelf sureReceiveWith:data];
                    }   break;
                    case 7 : { //再来一单
                        [weakSelf againOrderWith:data];
                    }   break;
                    case 8 : { //申请售后
                        [weakSelf applyAfterSalesWith:data];
                    }   break;
                }
            };
            cellModel;
        })];
    }
    [self.collectionView reloadData];
    
    if(!arrayM.count)
    {
        [self.collectionView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
    dicM[@"order_type"] = @"1";
    dicM[@"status"] = @(self.status);
    dicM[@"user_id"] = [RBAccountManager getUserId];
    return dicM;
}
- (NSString *)listRequestPath
{
    return _Order_list;
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBGoodsOrderResponse.class;
}
- (UIScrollView *)listView
{
    return self.collectionView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}
- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}

#pragma mark - Event
//支付
- (void)payOrderWith:(RBOrderListModel *)model {
    RBCashierVC *vc = RBCashierVC.new;
    vc.isFromOrder = YES;
    vc.order_id = model.order_id;
    vc.order_num = model.order_number;
    vc.order_price = model.total_price;
    LCWeakSelf;
    vc.block = ^{
        [weakSelf.listView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
//取消订单
- (void)cancelOrderWith:(RBOrderListModel *)model {
    LCWeakSelf;
    [JKAlert showTitle:@"提示" message:@"确定取消此订单?" cancelBtn:YES completion:^{
        [weakSelf viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"order_id"] = model.order_id;
        } path:_Order_cancel post:YES log:YES responseClass:RBResponse.class desView:weakSelf.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
            if (data.succeed) {
                [weakSelf.view showTip:@"取消成功" completion:nil];
                [weakSelf.listView.mj_header beginRefreshing];
            }
        } failBlock:^(RBResponse *data) {
            [weakSelf.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    }];
}
//查看订单
- (void)checkOrderWith:(RBOrderListModel *)model {
    RBGoodsOrderDetailVC *vc = RBGoodsOrderDetailVC.new;
    vc.order_id = model.order_id;
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}
//查看物流
- (void)checkLogisticsWith:(RBOrderListModel *)model {
    RBLogisticsVC *vc = RBLogisticsVC.new;
    vc.order_id = model.order_id;
    [self.navigationController pushViewController:vc animated:YES];
}
//去评价
- (void)appraiseWith:(RBOrderListModel *)model {
    RBPublishEvaluateVC *vc = RBPublishEvaluateVC.new;
    vc.goodsModel = ({
        RBPublishEvaluateGoodsModel *goodsModel = RBPublishEvaluateGoodsModel.new;
        goodsModel.price = model.total_price;
        goodsModel.title = model.goods_name;
        goodsModel.picurl = model.file;
        goodsModel.type = 1;
        goodsModel.order_goods_id = model.order_goods_id;
        goodsModel;
    });
    LCWeakSelf;
    vc.block = ^{
        [weakSelf getFirstPageData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
//确认收货
- (void)sureReceiveWith:(RBOrderListModel *)model {
    LCWeakSelf;
    [JKAlert showTitle:@"提示" message:@"确定收到货物?" cancelBtn:YES completion:^{
        [weakSelf viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"order_id"] = model.order_id;
            dicM[@"status"] = @"3";
        } path:_Order_update post:YES log:YES responseClass:RBResponse.class desView:weakSelf.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
            if (data.succeed) {
                [weakSelf.view showTip:@"收货成功" completion:nil];
                [weakSelf.listView.mj_header beginRefreshing];
            }
        } failBlock:^(RBResponse *data) {
            [weakSelf.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    }];
}
//再来一单
- (void)againOrderWith:(RBOrderListModel *)model {
}
//申请售后
- (void)applyAfterSalesWith:(RBOrderListModel *)model {
    RBAfterSalesModel *data = RBAfterSalesModel.new;
    if ([model.order_status isEqualToString:@"1"]) {
        [RBAfterSalesAlertView showRefundWith:data];
    } else if ([model.order_status isEqualToString:@"4"] || [model.order_status isEqualToString:@"3"]) {
        [RBAfterSalesAlertView showAfterSalesWith:data];
    } else {
        [RBAfterSalesAlertView showTwoBtnWith:data];
    }
}

#pragma mark RBGoodsOrderDetailDelegate
//刷新
- (void)refreshListAction {
    [self.listView.mj_header beginRefreshing];
}


#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegateAuto(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
            UICollectionView *view =  [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _collectionView;
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
